<?php $basePath=$this->basePath;?>
<?php $baseUrl=$this->baseUrl;?>
<script> 
var BASE_PATH = '<?php echo $basePath;?>';
var BASE_URL = '<?php echo $baseUrl;?>';
</script>
<? if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!="") {?>
<script>
	<? if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="user") {?>
		window.location=BASE_URL+'/profiles/userprofile';
	<? } else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="admin") {?>
		window.location=BASE_URL+'/profiles/admin-profile';
	<? } else if(isset($_SESSION['user']['user_type']) && $_SESSION['user']['user_type']=="superadmin") {?>
		window.location=BASE_URL+'/users/superadmin-first?hello_first=hello';
	<?}?>	
</script>
<?} else {?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Welcome to cbay.org</title>
	</head>
	<body class="login-page">
		<div class="bg-cover"></div>
		<div id="wrapper" class="container">
			<form class="form-login" role="form">

				<h1>Login</h1>
				<div class="login-area">

					<div class="input-group">
						<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
						<input type="text" placeholder="User Name" class="form-control input-lg" id="usernameLogin"/>
						
					</div>
					<span id="errorusernameLogin" style="color:red"></span>
					<div class="input-group">
						<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
						<input type="password" placeholder="*******" class="form-control input-lg" id="passwdLogin"/>
						
					</div>
					<span id="errorpasswdLogin" style="color:red"></span>
					<div class="form-group">
						<button class="btn btn-lg btn-danger btn-block" type="button" onclick="validateLogin()">
							Sign In
						</button><a href="<? echo $this->Url('forgetpassword');?>" class="center-block">Forgot Password?</a>
					</div>

				</div>
				

			</form>		
		</div>
	</body>
</html>
<div class="clear"></div>  
<script type="text/javascript" charset="utf-8">	
$('#passwdLogin').keypress(function(e){
	if( e.keyCode == 13 ){
		validateLogin();	
	}
});
function validateLogin(){
	var flag=true;	
	if($('#usernameLogin').val()==""){		
		$('#errorusernameLogin').html('*User name required');
		flag=false;
	}else{
		$('#errorusernameLogin').html('');
	}
	if($('#passwdLogin').val()==""){
		$('#errorpasswdLogin').html('*Password required');
		flag=false;
	}else{
		$('#errorpasswdLogin').html('');
	}
	if(flag==false){ 
		return false;
	}else{ 
		var userEmail=$('#usernameLogin').val();
		var userPassword=$('#passwdLogin').val();
		$.ajax({
			type:'POST',
			url:BASE_PATH+'/users/login',
			data:{userEmail:userEmail,userPassword:userPassword},
			success: function(data){ 
				if(data.output=='success'){
					var userid=data.user_id;
					$('#loginFailed').html('');
					if(data.user_type=='user'){
						window.location=BASE_URL+'/profiles/userprofile';	
					}else if(data.user_type=='admin'){
						window.location=BASE_URL+'/profiles/admin-profile';	
					}else if(data.user_type=='superadmin'){
						window.location=BASE_URL+'/users/superadmin-first?hello_first=hello';	
					}									
				}else if(data.output=='notsuccess'){
					$('#loginFailed').html('*Please enter valid username and password!');
				}
			}
		});
	}	
}
</script>
<?}?>